Excel vba 实例(8)

您所在的位置:网站首页 excel vba 正则表达式函数 Excel vba 实例(8)

Excel vba 实例(8)

2023-07-18 23:13| 来源: 网络整理| 查看: 265

'只保留中文的代码如下:

Function ZW(i As String) As String

Dim a As Object

Set a = CreateObject("VB.REGEXP")

a.Pattern = "[^\u4e00-\u9fa5]"

a.IgnoreCase = True

a.Global = True

ZW = a.Replace(i, "")

Set a = Nothing

End Function

'只保留数字的代码如下:

Function SZ(i As String) As String

Dim a As Object

Set a = CreateObject("VB.REGEXP")

a.Pattern = "[^0-9]"

a.IgnoreCase = True

a.Global = True

SZ = a.Replace(i, "")

Set a = Nothing

End Function

'只保留字母的代码如下:

Function ZM(i As String) As String

Dim a As Object

Set a = CreateObject("VB.REGEXP")

a.Pattern = "[^A-Z^a-z]"

a.IgnoreCase = True

a.Global = True

ZM = a.Replace(i, "")

Set a = Nothing

End Function

'只保留数字和字母的代码如下:

Function SZZM(i As String) As String

Dim a As Object

Set a = CreateObject("VB.REGEXP")

a.Pattern = "[^A-Z^a-z^0-9]"

a.IgnoreCase = True

a.Global = True

SZZM = a.Replace(i, "")

Set a = Nothing

End Function

演示一下调用函数的结果:

同样的,永恒君直接把上面实例VBA的文件分享出来,需要的就直接使用吧。

公号(见图片)后台回复“正则”即可。

不少人对VBA代码有抵触心理,其实没有那么复杂,把他看成一个稍微复杂点的函数即可。

当然如果并不打算深入学习VBA,只是临时使用下,那只要会复制黏贴就好了!

至于怎么用VBA代码,可以参考之前的文章:

Excel vba 实例(1) - 批量制作工资表头

Excel vba 实例(2) - 批量将工作表拆分为单独文件

Excel vba 实例(3) - 多个工作簿批量合并

Excel vba 实例(4) - 根据已有名称,批量新建表格

Excel vba 实例(5) - 快速合并n多个相同值的单元格

Excel vba 实例(6) - 一键汇总多个sheet数据到总表

Excel vba 实例(7)-一键批量打印工作簿

Excel VBA功能很强大,但是要熟练的运用起来还是有一定的难度,好在永恒君帮大家搜集了几套关于VBA的视频教程,非常的实用。

需要的话,公号(见图片)后台回复“VBA”获取吧~~

欢迎交流!返回搜狐,查看更多



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3